package com.bilibili.api;

import com.alibaba.fastjson.JSONObject;
import com.bilibili.api.support.UserSupport;
import com.bilibili.domain.JsonResponse;
import com.bilibili.domain.PageResult;
import com.bilibili.domain.entity.User;
import com.bilibili.domain.entity.UserInfo;
import com.bilibili.service.UserFollowingService;
import com.bilibili.service.UserInfoService;
import com.bilibili.service.UserService;
import com.bilibili.utils.RSAUtil;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;
import java.util.List;
import java.util.Map;

/**
 * projectName:com.bilibili.api
 *
 * @author 飞
 * @create by 2024/6/218:33
 * description:
 */
@RestController
@Api(tags = "用户相关接口")
public class UserController {
    @Resource
    private UserService userService;
    @Resource
    private UserSupport userSupport;
    @Resource
    private UserInfoService userInfoService;
    @Resource
    private UserFollowingService userFollowingService;
    @GetMapping("/users")
    @ApiOperation("通过token获取用户信息")
    public JsonResponse<User> getUserInfo(){
        Long userId = userSupport.getCurrentUserId();
        User user = userService.getById(userId);
        UserInfo userInfo = userInfoService.getByUserId(userId);
        user.setUserInfo(userInfo);
        return new JsonResponse<>(user);
    }

    @GetMapping("/rsa-pks")
    @ApiOperation("获取rsa公钥")
    public JsonResponse<String> getRsaPublicKey(){//获取rsa公钥
        String publicKeyStr = RSAUtil.getPublicKeyStr();
        return new JsonResponse<>(publicKeyStr);
    }

    @PostMapping("/users")
    @ApiOperation("用户注册")
    public JsonResponse<String> register(@RequestBody User user){
        userService.addUser(user);
        return JsonResponse.success();
    }

    @PostMapping("/user-tokens")
    @ApiOperation("用户登录")
    public JsonResponse<String> login(@RequestBody User user) throws Exception{
        String token = userService.login(user);
        return new JsonResponse<>(token);
    }

    /**
     * TODO:前后端联调修改不了
     * @param userInfo
     * @return
     */
    @PutMapping("/user-infos")
    @ApiOperation("修改用户信息")
    public JsonResponse<String> updateUserInfo(@RequestBody UserInfo userInfo)
    {
        Long userId = userSupport.getCurrentUserId();
        userInfo.setUserId(userId);
        userInfoService.updateUserInfo(userInfo);
        return JsonResponse.success();
    }

    @GetMapping("/user-infos")
    @ApiOperation("分页查询")
    public JsonResponse<PageResult<UserInfo>> pageListUserInfos(
            @RequestParam Integer num,
            @RequestParam Integer size,
            String nick)
    {
        Long userId = userSupport.getCurrentUserId();
        JSONObject params = new JSONObject();
        params.put("num",num);
        params.put("size",size);
        params.put("nick",nick);
        params.put("userId",userId);

        PageResult<UserInfo> pageResult= userService.pageListUserInfos(params);
        //判断是否关注
        if (pageResult.getTotal() > 0){
            List<UserInfo> checkedUserInfoList = userFollowingService.checkFollowingStatus(pageResult.getList(), userId);
            pageResult.setList(checkedUserInfoList);
        }
        return new JsonResponse<>(pageResult);
    }


    @PostMapping("/user-dts")
    @ApiOperation("用户登录（双token）")
    public JsonResponse<Map<String,Object>> loginForDts(@RequestBody User user) throws Exception{
        Map<String,Object> map = userService.loginForDts(user);
        return new JsonResponse<>(map);
    }

    @DeleteMapping("/refresh-tokens")
    @ApiOperation("退出登录")
    public JsonResponse<String> logout(HttpServletRequest request){
        String refreshToken = request.getHeader("refreshToken");
        Long userId = userSupport.getCurrentUserId();
        userService.logout(refreshToken,userId);
        return JsonResponse.success();
    }

    @PostMapping("/access-tokens")
    @ApiOperation("刷新token")
    public JsonResponse<String> refreshAccessToken(HttpServletRequest request) throws Exception {
        String refreshToken = request.getHeader("refreshToken");
        String accessToken = userService.refreshAccessToken(refreshToken);
        return new JsonResponse<>(accessToken);
    }
}
